package me.ezitku.base.extensions

import android.graphics.Color
import android.view.View
import android.widget.PopupWindow
import android.widget.TextView
import com.ug_project.views.click
import com.ug_project.views.content
import com.ug_project.views.findTextView
import com.ug_project.views.findView
import me.ezitku.base.R

@Suppress("UNCHECKED_CAST")
/**
 *  0دىن 9 غىچە سان
 *  يەنى ھېسابلىغۇچقا ئوخشاش
 */
class CalculaView<T>(var textView: TextView
                     , var layoutId: Int
                     , var result: (T) -> Unit = {}) : PopupWindow() {

    val NUMBER_STRING = 0
    //ئەڭ يېڭى كىرگۈزگەن قىممەت
    private var text = textView.content
    //ئەسلى تېكىست
    private var orginalText = text
    //تۇنجى كىرگۈزگەندە بۇرۇنقىنى تازلاپ يېڭىنى كىرگۈزسۇنمۇ
    var isFirstClear = true
    //كىرگۈزگەن ساننىڭ ئاخىردا ئايلىنىدىغان تىپى
    var resultType: Any = Int

    var mWidth = 380
    var mHeight = 400
    /**
     * سۈكۈتتە مۇشۇ TextView نىڭ ئاستىدىلا چىقىدۇ
     * بولمىسا showDropDown فۇنكىسيەسىنى قايتا يېزىپ چىقىدىغان ئورۇننى كۆرستىمىز
     */
    var showDefault = true
    //TextView نى تاللىغاندا تېكىستنىڭ رەڭگى قايسى رەڭدە بولىدۇ
    var selectTextColor = Color.GREEN
    //ئەسلى تېكىستنىڭ رەڭنى ساقلاپ قويىدۇ
    var orginalTextColor = textView.currentTextColor

    var _onDismis: (() -> Unit)? = null

    fun init() {
        textView.setTextColor(selectTextColor)

        if (isFirstClear) {
            text = ""
        } else {
            text = textView.content
        }
        width = mWidth
        height = mHeight
        isFocusable = false
        isOutsideTouchable = false
        update()

        var rootView = layoutId.inflate
        contentView = rootView

//      showAsDropDown(search,search.width / 2 ,search.height/2 - 420 / 2 )


        class ClickListener : View.OnClickListener {
            override fun onClick(v: View?) {
                text += (v as TextView).content
                //تۇنجىسى چىكىت بوپ بولسا يول قويمايدۇ
                if (!text.startsWith(".")) {
                    textView.content = text
                }

            }
        }
        rootView.findTextView(R.id.btn_0).setOnClickListener(ClickListener())
        rootView.findTextView(R.id.btn_1).setOnClickListener(ClickListener())
        rootView.findTextView(R.id.btn_2).setOnClickListener(ClickListener())
        rootView.findTextView(R.id.btn_3).setOnClickListener(ClickListener())
        rootView.findTextView(R.id.btn_4).setOnClickListener(ClickListener())
        rootView.findTextView(R.id.btn_5).setOnClickListener(ClickListener())
        rootView.findTextView(R.id.btn_6).setOnClickListener(ClickListener())
        rootView.findTextView(R.id.btn_7).setOnClickListener(ClickListener())
        rootView.findTextView(R.id.btn_8).setOnClickListener(ClickListener())
        rootView.findTextView(R.id.btn_9).setOnClickListener(ClickListener())
        rootView.findTextView(R.id.btn_point).click {
            if (text != "" && resultType != Int && resultType != NUMBER_STRING) {
                if (!text.contains(".")) {
                    text += it.content
                    textView.content = text
                }
            }
        }
        rootView.findView(R.id.btn_del).click {
            var tx = textView.content
            if (tx != "") {
                text = tx.substring(0, tx.length - 1)
                textView.content = text
            }


        }

        setOnDismissListener {
            textView.setTextColor(orginalTextColor)
            var result = when (resultType) {
                Double -> {
                    try {
                        var content = textView.content.toDouble()
                        result(content as T)
                        "$content"
                    } catch (e: Exception) {
                        textView.content =   orginalText
                    }
                }
                Float -> {
                    try {
                        var content = textView.content.toFloat()
                        result(content as T)
                        "$content"
                    } catch (e: Exception) {
                        textView.content =  orginalText
                    }
                }
                Int -> {
                    try {
                        var content = textView.content.toInt()
                        result(content as T)
                        "$content"
                    } catch (e: Exception) {
                        textView.content =  orginalText
                    }
                }
                NUMBER_STRING -> {
                    var content = textView.content
                    result(content as T)
                    content
                }
                String -> {
                    try {
                        var content = textView.content
                        result(content as T)
                        content
                    } catch (e: Exception) {
                        textView.content = orginalText
                    }
                }
                else -> ""
            }

//            if (result != "") {
//                textView.content = result
//            } else {
//                textView.content = orginalText
//            }


            _onDismis?.invoke()

        }
        if (showDefault)
            showAsDropDown(textView)
    }

    fun onDismis(action: () -> Unit) {
        _onDismis = action
    }
}

fun <T> TextView.calculaView(action: CalculaView<T>.() -> Unit): PopupWindow {
    val temp = CalculaView<T>(this, R.layout.calcula_layout)
    action(temp)
    temp.init()
    return temp
}

fun <T> TextView.calculaView(action: CalculaView<T>.() -> Unit, result: (T) -> Unit): PopupWindow {
    val temp = CalculaView<T>(this, R.layout.calcula_layout, result)
    action(temp)
    temp.init()
    return temp
}


fun <T> TextView.calculaViewSmall(action: CalculaView<T>.() -> Unit, result: (T) -> Unit): PopupWindow {
    val temp = CalculaView<T>(this, R.layout.calcula_layout_small, result)
    action(temp)
    temp.init()
    return temp
}

//fun TextView.calculaViewSmall(action: CalculaView.() -> Unit): PopupWindow {
//    val temp = CalculaView(this, R.layout.calcula_layout_small)
//    temp.mWidth = 300
//    temp.mHeight = 280
//    action(temp)
//
//    temp.init()
//    return temp
//}